package com.haidechizi.designpattern.strategy.extension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author haidechizi
 */
@Component
public class BarCodeFactory2 {

    private static final Map<String, BarCodeAnalysis> analysisHolder = new ConcurrentHashMap<>();

    @Autowired
    public void init(List<AbstractBarCodeAnalysis> barCodeAnalysisList) {
        barCodeAnalysisList.forEach(barCodeAnalysis -> {
            analysisHolder.put(barCodeAnalysis.type(), barCodeAnalysis);
        });
    }

    public static BarCodeAnalysis getAnalysis(String type) {
        return Optional.ofNullable(analysisHolder.get(type)).orElse(analysisHolder.get("default"));
    }
}
